package com.shiro.test.mvc.controller;

import com.shiro.test.mvc.filter.MyShiroFilterFactoryBean;
import com.shiro.test.mvc.pojo.Role;
import com.shiro.test.mvc.service.IRoleService;
import com.shiro.test.mvc.util.MessageUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/11/23 0023.
 */
@Controller
@RequestMapping("role.html")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @RequestMapping(params = "act=page")
    @RequiresPermissions("5")
    public String page(){
        return "role_list";
    }
    @RequestMapping(params = "act=go_edit")
    public String go_edit(){
        return "role_edit";
    }
    @RequestMapping(params = "act=list")
    @ResponseBody   //该注解限制返回的数据时JSON格式。
    public List getRoleList(){
        List<Role> roles = roleService.getRoleList();
        return roles;
    }
    @RequestMapping(params = "act=edit")
    @ResponseBody   //该注解限制返回的数据时JSON格式。
    public Map<String,Object> doEdit(Role role){
        String remark = role.getRemark();

        if (role.getId() == null){
            roleService.insert(role);
        }else {
            roleService.updateByPrimaryKey(role);
        }

        return MessageUtil.getSuccessResult("编辑成功");
    }

    @RequestMapping(params = "act=role_menu")
    @ResponseBody   //该注解限制返回的数据时JSON格式。
    public List<Integer> roleMenu(int roleId){
        return roleService.getRoleMenuIds(roleId);
    }

    @RequestMapping(params = "act=assign_menu")
    @ResponseBody   //该注解限制返回的数据时JSON格式。
    public Map<String,Object> assignMenu(int roleId,int[] menuIds){
        roleService.addRoleMenu(roleId,menuIds);
        return MessageUtil.getSuccessResult("编辑成功");
    }
}
